package com.yxcz.serviceImpl;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipInputStream;
import javax.annotation.Resource;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.yxcz.service.WorkFlowService;


@Service
public class WorkFlowServiceImpl implements WorkFlowService {
	
	@Resource
	RepositoryService repositoryService ;
	
	@Resource
	RuntimeService runtimeService ;

	@Override
	public void DeployNewProcess(MultipartFile file) {
		// TODO Auto-generated method stub
		try {
			repositoryService.createDeployment().addZipInputStream(new ZipInputStream(file.getInputStream())).deploy();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public List<ProcessDefinition> listAllProcessDefinition() {
		// TODO Auto-generated method stub
		return repositoryService.createProcessDefinitionQuery().list();
	}

	@Override
	public void delete(String id) {
		// TODO Auto-generated method stub
		repositoryService.deleteDeployment(id,true);
	}

	@Override
	public InputStream viewDiagram(String id) {
		// TODO Auto-generated method stub
		return repositoryService.getProcessDiagram(id);
	}

	@Override
	public ProcessInstance startProcess(String key) {
		// TODO Auto-generated method stub
		return runtimeService.startProcessInstanceByKey(key);
	}

	@Override
	public ProcessInstance startProcess(String key, String bussinessKey) {
		// TODO Auto-generated method stub
		return runtimeService.startProcessInstanceByKey(key,bussinessKey+"");
	}
	

}
